﻿@Cofe ?= {}

$ = jQuery
Entry = Cofe.Entry
EntryItemList = Cofe.EntryItemList
EntryItem = Cofe.EntryItem
EntryList = Cofe.EntryList
EntryTreeItem = Cofe.EntryTreeItem
Link = Cofe.Link
utils = Cofe.utils



class EntryUploadView extends Spine.Controller    
  elements : 
    "#uploadForm" : "uiUploadForm"
    "#uploadForm :file" : "uiUploadFile"
    "#uploadForm :button" : "uiSubmitButton"
    "#output" : "uiOutput"
  
  events : 
    #"submit #uploadForm" : "submit"
    "click #uploadForm :button" : "submit"

  submit : (event) =>    
    console.log "submit"
    if @uiUploadFile[0].files.length > 0     
      
       formData = new FormData();
       formData.append('file', @uiUploadFile[0].files[0])
       $.ajax({
         url: @entryRootItem.links.self.url + "/upload"
         type: 'POST'
         data : formData
         cache: false
         contentType : false
         processData : false 
         success : () => console.log "success"
         error : () => console.log "error"
       })
    return false
  
  changeRootEntry : (entryRootItem) =>
    #to-do: check entryRootItem, disable upload if not Cofe.IO.DirectoryInfo
    @entryRootItem = entryRootItem
    @

  init : () ->
    @

Cofe.EntryUploadView = EntryUploadView